<?php

namespace app\api\controller\project;
use app\BaseController;
use app\api\service\project\ProjectTrunkService;
use app\util\CommonUtil;
use app\api\service\develop\DatagridColumnService;
use app\api\service\log\AdminOperateLogService;
use app\util\AdminUserUtil;
use app\util\ResponseUtil;
use think\App;

class ProjectTrunk extends BaseController
{

    private $datagridCode = 'page_toolbar';
    private $arrFieldTitle = array("status" => "状态");

    public function __construct(App $app)
    {
        parent::__construct($app);
        $servicePageColumn = new DatagridColumnService();
        $list = $servicePageColumn->getListNoPage(array('datagrid_code' => $this->datagridCode, 'status' => 1));
        $this->arrFieldTitle = array_column($list, 'column_title', 'column_name');
    }

    public function getProjectTrunkInfo()
    {
        $id = $this->requestData['id'];
        $service = new ProjectTrunkService();
        $finalData = $service->findRowById($id);
        return ResponseUtil::success($finalData);
    }

    public function getProjectTrunkList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new ProjectTrunkService();
        $list = $service->getListWithPage($this->requestData, $pageIndex, $pageSize);
        $total = $service->getTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getProjectTrunkOption()
    {
        $service = new ProjectTrunkService();
        $list = $service->getListNoPage($this->requestData);
        return ResponseUtil::success($list);
    }

    public function addProjectTrunk()
    {
        try{
            if(empty($this->requestData['datagrid_code'])){
                return ResponseUtil::failure("数据表标识码不可为空");
            }
            $datagridCode = $this->requestData['datagrid_code'];

            $servicePageColumn = new DatagridColumnService();
            $columnList = $servicePageColumn->getListNoPage(['datagrid_code' => $datagridCode, 'is_in_add'=>1, 'is_require' => 1]);
            foreach($columnList as $columnItem){
                if(empty($this->requestData[$columnItem['column_name']])){
                    return ResponseUtil::failure($columnItem['column_title']."不可为空");
                }
            }

            $service = new ProjectTrunkService();
            $res = $service->createRow($this->requestData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($this->requestData, $this->arrFieldTitle);
                $message = '新增项目';
                if($addInfo){
                    $message .= ','.$addInfo;
                }
                $logData = [
                    'subject_id' => $res,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ProjectTrunk', AdminOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增项目失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增项目失败");
        }
    }

    public function editProjectTrunk()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的项目");
            }

            $id = $this->requestData['id'];
            $service = new ProjectTrunkService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑项目,'.$changeInfo['update_info'];
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ProjectTrunk', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑项目失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑项目失败");
        }
    }

    public function deleteProjectTrunk()
    {
        try{
            $id = $this->requestData['id'];
            $service = new ProjectTrunkService();
            $info = $service->findRowById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }

            $res = $service->removeRowById($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($info, $this->arrFieldTitle);
                $message = '删除项目';
                if($deleteInfo){
                    $message .= ','.$deleteInfo;
                }
                $logData = [
                    'subject_id' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ProjectTrunk', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除项目失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除项目失败");
        }
    }

    public function deleteMultiProjectTrunk(){
        try{
            $arrId = $this->requestData['arr_id'];
            if(!is_array($arrId)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new ProjectTrunkService();
            $deleteData = [];
            foreach($arrId as $id){
                $info = $service->findRowById($id);
                if(!$info){
                    return ResponseUtil::failure("项目不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->removeListByManyId($arrId);
            if($res){
                $message = '删除项目';
                $logData = [
                    'subject_id' => json_encode($arrId),
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ProjectTrunk', AdminOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除项目失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除项目失败");
        }
    }

    public function simpleUpdateProjectTrunk()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的项目");
            }

            $id = $this->requestData['id'];
            $service = new ProjectTrunkService();
            $oldInfo = $service->findRowById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->updateRowById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData, $this->arrFieldTitle);
                $message = '编辑项目,'.$changeInfo['update_info'];
                $logData = [
                    'subject' => $id,
                    'remark'  => '',
                    'description' => $message,
                ];
                AdminUserUtil::addOperateLog('ProjectTrunk', AdminOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑项目失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog(__CLASS__, __FUNCTION__,  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑项目失败");
        }
    }

}



